package cn.lianwei.yonyong.controller;

import cn.lianwei.yonyong.dao.TestDao;
import cn.lianwei.yonyong.pojo.Xsjbxx;
import cn.lianwei.yonyong.pojo.SysUser;
import cn.lianwei.yonyong.service.UserService;
import cn.lianwei.yonyong.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    //登陆界面
    @RequestMapping("/")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestParam(value="tel") String tel, @RequestParam(value="password") String password, HttpServletRequest request){

        SysUser user=new SysUser();
        user.setUsername(tel);
        user.setPwd(MD5Utils.getMD5(password));

        SysUser rs_user=userService.queryUser(user);

        if (rs_user != null) {
            HttpSession session=request.getSession();
            session.setAttribute("rs_user", rs_user);
            String jsonStr = "{\"errorCode\":\"00\",\"errorMessage\":\"登陆成功！\"}";
            return jsonStr;
        } else {
            String jsonStr = "{\"errorCode\":\"11\",\"errorMessage\":\"该用户不存在\"}";
            return jsonStr;
        }
    }
}



























